package posts_api

import (
	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson/primitive"

	"software/models/res"
	"software/service"
)

// ContentAuditRequest 内容审核请求
type ContentAuditRequest struct {
	Content string `json:"content" binding:"required"`
	Title   string `json:"title"`
}

// ContentAuditResponse 内容审核响应
type ContentAuditResponse struct {
	Passed      bool   `json:"passed"`
	Label       string `json:"label,omitempty"`
	TitlePassed bool   `json:"title_passed"`
	TitleLabel  string `json:"title_label,omitempty"`
}

// AuditContent 审核内容
func (p PostsApi) AuditContent(c *gin.Context) {
	var req ContentAuditRequest

	// 绑定请求参数
	if err := c.ShouldBindJSON(&req); err != nil {
		res.FailWithMsg("参数错误: "+err.Error(), c)
		return
	}

	// 创建内容审核服务
	contentAuditService := service.ContentAuditService{}

	// 审核内容
	passed, label, err := contentAuditService.AuditText(req.Content)
	if err != nil {
		res.FailWithMsg("内容审核失败: "+err.Error(), c)
		return
	}

	// 初始化响应
	response := ContentAuditResponse{
		Passed:      passed,
		Label:       label,
		TitlePassed: true,
	}

	// 如果有标题，也审核标题
	if req.Title != "" {
		titlePassed, titleLabel, err := contentAuditService.AuditText(req.Title)
		if err != nil {
			res.FailWithMsg("标题审核失败: "+err.Error(), c)
			return
		}
		response.TitlePassed = titlePassed
		response.TitleLabel = titleLabel
	}

	res.OKWithData(response, c)
}

// AuditPost 审核帖子
func (p PostsApi) AuditPost(c *gin.Context) {
	// 获取帖子ID
	postIDStr := c.Param("id")

	// 将ID转换为ObjectID
	objectID, err := primitive.ObjectIDFromHex(postIDStr)
	if err != nil {
		res.FailWithMsg("无效的帖子ID格式", c)
		return
	}

	// 获取帖子详情
	post, err := service.GetPostDetail(c, objectID)
	if err != nil {
		res.FailWithMsg("获取帖子详情失败: "+err.Error(), c)
		return
	}

	// 获取帖子内容和标题
	content, ok := post["content"].(string)
	if !ok {
		res.FailWithMsg("帖子内容格式错误", c)
		return
	}

	title, ok := post["title"].(string)
	if !ok {
		res.FailWithMsg("帖子标题格式错误", c)
		return
	}

	// 检查标题是否为空
	if title == "" {
		res.FailWithMsg("帖子标题不能为空", c)
		return
	}

	// 创建内容审核服务
	contentAuditService := service.ContentAuditService{}

	// 内容可以为空，如果不为空则审核内容
	contentPassed := true
	contentLabel := ""
	if content != "" && content != "无内容" {
		contentPassed, contentLabel, err = contentAuditService.AuditText(content)
		if err != nil {
			res.FailWithMsg("内容审核失败: "+err.Error(), c)
			return
		}
	}

	// 审核标题
	titlePassed, titleLabel, err := contentAuditService.AuditText(title)
	if err != nil {
		res.FailWithMsg("标题审核失败: "+err.Error(), c)
		return
	}

	// 只有当内容和标题都通过时，帖子才通过审核
	passed := contentPassed && titlePassed

	// 根据审核结果确定推荐状态，但不自动更新
	var status int
	if passed {
		status = 1 // 正常
	} else {
		status = 2 // 封禁
	}

	// 设置消息
	var message string
	if passed {
		message = "帖子审核通过，请放行"
	} else {
		message = "帖子内容违规，请封禁"
	}

	// 返回审核结果
	response := map[string]interface{}{
		"post_id":        objectID.Hex(),
		"passed":         passed,
		"content_passed": contentPassed,
		"content_label":  contentLabel,
		"title_passed":   titlePassed,
		"title_label":    titleLabel,
		"status":         status,
		"message":        message,
	}

	res.OKWithData(response, c)
}

// FeedbackAudit 反馈审核结果
func (p PostsApi) FeedbackAudit(c *gin.Context) {
	type FeedbackRequest struct {
		TaskID  string `json:"task_id" binding:"required"`
		Content string `json:"content" binding:"required"`
		Label   string `json:"label" binding:"required"`
	}

	var req FeedbackRequest

	// 绑定请求参数
	if err := c.ShouldBindJSON(&req); err != nil {
		res.FailWithMsg("参数错误: "+err.Error(), c)
		return
	}

	// 创建内容审核服务
	contentAuditService := service.ContentAuditService{}

	// 反馈审核结果
	err := contentAuditService.TextFeedback(req.TaskID, req.Content, req.Label)
	if err != nil {
		res.FailWithMsg("反馈审核结果失败: "+err.Error(), c)
		return
	}

	res.OK(c)
}
